{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **作业：尝试最优的高阶拟合**\n",
    "已知二维平面上一组坐标点(x, y)，这些坐标点的图像近似于正弦曲线。请尝试分别使用1~9阶曲线进行最小二乘拟合，并综合考量选择最优的拟合阶数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **任务1：生成和查看样本数据点**\n",
    "下面的代码中，变量x_origin和y_origin存放了样本数据点的横坐标和纵坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFRVJREFUeJzt3XGsnXd93/H3p8YJV+06O42hthOToEUedFTxdpZNYuq6kmC3mmK3ZW2iTTUbldWpWbtVWMRjGlooapilUW2iGx6khJYRWjDGU4tMSGDdpIbmuk7jJPQSE8TiezPiEtwO5Q5s890f97npeW7O9b3X5/g+9/q+X9LReZ7f83vO8z2JfT5+fs/vnCdVhSRJs76n6wIkSSuLwSBJajEYJEktBoMkqcVgkCS1GAySpBaDQZLUYjBIkloMBklSyyu6LuBSXHvttXXDDTd0XYYkrSrHjx//s6ratFC/VRkMN9xwA+Pj412XIUmrSpKvLaafQ0mSpBaDQZLUYjBIkloMBklSi8EgSWoxGCRJLQaDJKllVX6PQdLlc+TEJAePTTB1dpotG8bYv3M7e3Zs7bosLSODQdJLjpyY5MDhk0yfuwDA5NlpDhw+CWA4rCEjGUpKcl+S55M8Mc/2JPmPSU4leTzJ3+zbtjfJ081j7yjqkXRpDh6beCkUZk2fu8DBYxMdVaQujOoaw4eBXRfZ/uPATc1jH/CfAZJcA7wL+DvALcC7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KeBq4BtN0yOraVYVQJKfBP4TsAk4CzxWVTu7rWphSX4C+HVgHXBfVb2n45IWLcnHgB9l5pc9vw68q6o+1GlRS5Dk7wH/EzjJzN9dgH9dVb/fXVVLk+SHgfuZ+fPzPcDvVNU9ndRiMEiS+jmUJElqMRgkSS0GgySpxWCQJLUYDJKkFoNBktRiMEiSWgwGSVLL/wdCpAOkHJxjcgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1fb75e93b00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import scipy.optimize as opt\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "num_origins = 10    # 原始数据点的采样数量。该数量应大于拟合曲线的阶数\n",
    "x_origin = np.linspace(-np.pi, np.pi, num_origins)  # 原始数据点的x坐标\n",
    "y_origin = np.sin(x_origin) + np.random.randn(num_origins) * 0.1     # 原始数据点的y坐标，并且添加了一个小的扰动\n",
    "\n",
    "# 查看原始数据点的分布情况\n",
    "plt.scatter(x_origin, y_origin)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **任务2：编写代码考察采用不同的高阶拟合效果**\n",
    "分别用3阶、5阶、7阶、9阶函数进行拟合，并对拟合结果进行可视化，观察最优的拟合阶数。   \n",
    "注意：并不是阶数越高越好。例如下面的结果，在高阶情况下有可能导致很明显的过拟合；\n",
    "![](../images/0505A01.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### TODO：在此处编写代码，分别尝试3、5、7、9阶拟合，并作出拟合效果图(参考上图)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
